# -*- coding: utf-8 -*-
"""
file:ch5_2二次函数与多点切线
author: Fuwen
time: 2022/7/16 15:06
software:PyCharm
"""
import matplotlib.pyplot as plt
import numpy as np

# 二次函数 f(x) = 3*x**2 - 12*x +10
a, b, c = 3, -12, 10

# 函数图像
x = np.linspace(-10, 14, 100)
y = [(a*i**2 + b*i + c) for i in x ]
plt.plot(x, y, '--')

# f(x) 的导函数 f′(x) = 6 * x - 12
# 绘制二次函数曲线上多个点的切线、微分
for x_loc in range(-5,10):
    y_loc = a*x_loc**2 + b*x_loc + c
    slope = 6 * x_loc - 12
    # 取2个坐标点以上构成切线的斜率
    x_new = [x_loc-5, x_loc, x_loc+5]
    y_new = [slope * (xn - x_loc) + y_loc for xn in x_new]
    plt.plot(x_new, y_new)

plt.grid()
plt.show()